package com.huawei.userservice.controller;


import com.huawei.userservice.pojo.Result;
import com.huawei.userservice.pojo.User;
import com.huawei.userservice.service.UserService;
import org.bouncycastle.asn1.x509.UserNotice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;


    //1 测试接口
    @GetMapping("/{id}")
    public String getUserId(@PathVariable("id") Integer id){
        System.out.println("user-service");
        return "user-id " + id;
    }

    @PostMapping("/test")
    public void tests(String username,String password){
        System.out.println(username);
    }

    //2 注册
    @PostMapping("/register")
    public Result register(String username, String password){
        //用户名是否背占用
        User user = userService.findByUsername(username);
        if(user == null){
            userService.register(username,password);
            return Result.success();
        }else{
            return Result.error("用户名已占用");
        }
    }
}
